package com.real8.plusplayground.client.miscellaneous;

import com.google.api.gwt.client.impl.ClientGoogleApiRequestTransport;
import com.google.api.gwt.services.plus.shared.Plus;
import com.google.api.gwt.services.plus.shared.Plus.ActivitiesContext.ListRequest.Collection;
import com.google.api.gwt.services.plus.shared.model.ActivityFeed;
import com.google.api.gwt.services.plus.shared.model.Person;
import com.google.api.gwt.shared.GoogleApiRequestTransport;
import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.real8.plusplayground.client.ClientFactory;

/**
 * Activity wrapper around some initial attempts inspired by example on <a href=
 * "http://code.google.com/p/gwt-google-apis/source/browse/trunk/apis/samples/plus/com/google/api/gwt/samples/plus/shared/PlusBase.java"
 * >gwt-google-apis</a>
 * 
 */
public class MiscellaneousActivity extends AbstractActivity {
	private final ClientFactory clientFactory;

	public MiscellaneousActivity(ClientFactory clientFactory,
			MiscellaneousPlace place) {
		this.clientFactory = clientFactory;
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		final Button b = new Button(
				"Authenticate to get your public activities");
		b.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				clientFactory.login(new Receiver<String>() {
					@Override
					public void onSuccess(String accessToken) {
						initializeTransport(
								new ClientGoogleApiRequestTransport(),
								accessToken);
					}
				});
			}
		});

		final Button b2 = new Button("Get public activities of user "
				+ getUserId());
		b2.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				initializeTransport(new ClientGoogleApiRequestTransport());
			}
		});

		HTMLPanel htmlPanel = new HTMLPanel("");
		htmlPanel.add(b);
		htmlPanel.add(b2);

		panel.setWidget(htmlPanel);
	}

	private void initializeTransport(GoogleApiRequestTransport impl,
			String authorizationToken) {
		clientFactory.createNewPlus(authorizationToken, new Receiver<Plus>() {
			@Override
			public void onSuccess(Plus plus) {
				getMe(plus, "me");
			}
		});
	}

	private void initializeTransport(GoogleApiRequestTransport impl) {
		clientFactory.createNewPlus(new Receiver<Plus>() {
			@Override
			public void onSuccess(Plus plus) {
				getMe(plus, getUserId());
			}
		});
	}

	public void getMe(final Plus plus, final String userId) {
		plus.people().get(userId).to(new Receiver<Person>() {
			@Override
			public void onSuccess(Person person) {
				println("Hello " + person.getDisplayName());

				getMyActivities(plus, userId);
			}
		}).fire();
	}

	private String getUserId() {
		return "103681118936608513069"; // Rutger van der Eijk
	}

	public void getMyActivities(Plus plus, String userId) {
		plus.activities().list(userId, Collection.PUBLIC)
				.to(new Receiver<ActivityFeed>() {
					@Override
					public void onSuccess(ActivityFeed feed) {
						println("===== PUBLIC ACTIVITIES =====");
						if (feed.getItems() == null
								|| feed.getItems().isEmpty()) {
							println("You have no public activities");
						} else {
							for (com.google.api.gwt.services.plus.shared.model.Activity a : feed
									.getItems()) {
								println("id=" + a.getId() + ", title = "
										+ a.getTitle());
							}
						}
					}
				}).fire();
	}

	private void println(String value) {
		clientFactory.getShell().println(value);
	}
}
